<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use Cookie;
use Hash;
use App\Http\Controllers\Controller;
//模型
use App\Models\Role;

class AdminController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
        $admin = array();
        if ($request->input('admin_id')) {
            $admin['a.id'] = $request->input('admin_id');
        }
        // dd($admin);
        $admin['a.agent_id'] = Cookie::get('logon')['agent_id'];
        $admins = DB::table('admin as a')
                -> leftJoin('role as r','r.id','=','a.role_id')
                -> select(
                    'a.id',
                    'a.account',
                    'a.name',
                    DB::raw(
                        "REPLACE(phone, SUBSTR(phone,4,4), '****') as mobile"
                    ),
                    'r.name as role_name',
                    'a.status'
                )
                -> where($admin)
                -> paginate(10);
                // dd($admins);
        return view('admin.admin.index',['admins' => $admins]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getCreate(Request $request)
    {
        $agent_id = $request -> cookie('logon')['agent_id'];
        //获取角色数据
        $roleInfo = Role::select('id','name') -> where('agent_id',$agent_id) -> get();
        return view('admin.admin.create',['roleInfo'=>$roleInfo]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postStore(Request $request)
    {
        $data = $request->except('_token');
        $data['agent_id'] = Cookie::get('logon')['agent_id'];
        $data['pwd'] = Hash::make($data['pwd']);
        $raw = DB::table('admin') -> insert($data);
        if ($raw) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    // public function getEdit($id)
    public function getEdit(Request $request)
    {
        $id = $request -> input('id');
        $admins = DB::table('admin') -> where('id',$id) -> first();
        //获取角色数据
        $roleInfo = Role::select('id','name') -> get();
        return view('admin.admin.edit',['admins'=>$admins,'roleInfo'=>$roleInfo]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postUpdate(Request $request)
    {
        $id = $request->input('id');
        $admin = $request->except('_token','id');
        $raw = DB::table('admin') -> where('id',$id) -> update($admin);
        return $raw;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getDestroy(Request $request)
    {
        $count = $request->input('account');
        $raw = DB::table('admin') -> where('account',$count) -> delete();
        if ($raw) {
            return 1;
        } else {
            return 0;
        }
    }

    public function getStatus(Request $request)
    {
        $status = $request->input('status');
        $account = $request->input('account');
        $raw = DB::table('admin') -> where('account',$account) ->update(['status'=>$status]);
        return $raw;
    }
}
